package com.zsrt.oa.ticket.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zsrt.oa.ticket.dao.TicketDao;
import com.zsrt.oa.ticket.dao.impl.TicketDaoImpl;
import com.zsrt.oa.ticket.entity.TicketEntity;
import com.zsrt.oa.ticket.service.TicketService;

/**
 * 门票的服务层实现类
 * @author 罗智壕-13424431416
 *
 * 2017年6月9日
 */
public class TicketServiceImpl implements TicketService{
	
	/**
	 * 初始化门票持久层
	 */
	private TicketDao dao = new TicketDaoImpl();
	
	/**
	 * 门票的增加
	 * @param ticketName
	 * @param ticketPrice
	 * @param ticketStatus
	 * @param map
	 */
	@Override
	public void ticketAdd(String ticketName, String ticketPrice, Map<String, Object> map) {
		TicketEntity entity = new TicketEntity();
		entity.setTicketName(ticketName);
		entity.setTicketPrice(Double.valueOf(ticketPrice));
		try {
			int result = dao.ticketAdd(entity);
			if(result == 1){
				map.put("result", "success");
				map.put("message", "增加成功...");
			}else{
				map.put("result", "failure");
				map.put("message", "增加失败...");
			}
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
	
	/**
	 * 查找全部门票
	 * @param start 从第几条开始
	 * @param end 到第几条结束
	 * @param map
	 */
	@Override
	public void ticketQuery(String start, String end, Map<String, Object> map) {
		List<TicketEntity> list = new ArrayList<TicketEntity>();
		try {
			list = dao.ticketQuery(Integer.parseInt(start), Integer.parseInt(end));
			map.put("result", "success");
			map.put("message", "查询成功...");
			map.put("data", list);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除门票
	 * @param ticketId
	 * @param map
	 * @return
	 */
	@Override
	public void ticketDelete(String ticketId, Map<String, Object> map) {
		try {
			dao.ticketDelete(Integer.parseInt(ticketId));
			map.put("result", "success");
			map.put("message", "删除成功...");
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据id指定查询
	 * @param ticketId
	 * @param map
	 */
	@Override
	public void ticketFindById(String ticketId, Map<String, Object> map) {
		List<TicketEntity> list = new ArrayList<TicketEntity>();
		try {
			list = dao.ticketByFindId(Integer.parseInt(ticketId));
			map.put("result", "success");
			map.put("message", "查询成功...");
			map.put("data", list);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
	
	/**
	 * 门票修改
	 * @param ticketName
	 * @param ticketId
	 * @param ticketStatus
	 * @param ticketPrice
	 * @param map
	 */
	@Override
	public void ticketUpdate(String ticketName, String ticketId, String ticketStatus, String ticketPrice,
			Map<String, Object> map) {
		
		TicketEntity entity = new TicketEntity();
		entity.setTicketName(ticketName);
		entity.setTicketPrice(Double.valueOf(ticketPrice));
		entity.setTicketId(Integer.parseInt(ticketId));
		entity.setTicketStatus(Integer.parseInt(ticketStatus));
		try {
			int result = dao.ticketUpdate(entity);
			if(result == 1){
				map.put("result", "success");
				map.put("message", "修改成功...");
			}else{
				map.put("result", "failure");
				map.put("message", "修改失败...");
			}
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
}
